var key,ws,spage,order_sn='';
var app = angular.module('app',[]);
app.controller('appcontroller',function($scope,$http){
	$scope.initView = function($data){
		console.log(JSON.stringify($data));
		$scope.orderlist = $data[0];
		$scope.pics = pics;
		$scope.userpic = userpic;
	}
	$scope.cancel = function (order_id) {      //取消订单
		plus.nativeUI.showWaiting('进行中...');
		$http({
			method:"post",
			url:apiRoot + "?act=seller_order&op=order_cancel",
			data:{
				key:key,
				order_id:order_id,
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
             }
		}).then(function SuccessCallback(info){ 
			console.log(JSON.stringify(info.data.code));
			plus.nativeUI.closeWaiting();
			if(info.data.code == 200){
				toast('取消成功！');
				plus.webview.getWebviewById(spage).reload();
				setTimeout(function(){
					ws.close();
				},1500)
			}else{
				toast(info.data.datas.error);
			}
						
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();
			error();
		}) 
	}
	$scope.distribution = function (order_id) {      //配送
		plus.nativeUI.showWaiting('进行中...');
		$http({
			method:"post",
			url:apiRoot + "?act=seller_order&op=order_deliver_send",
			data:{
				key:key,
				order_id:order_id,
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
             }
		}).then(function SuccessCallback(info){ 
			console.log(JSON.stringify(info.data.code));
			plus.nativeUI.closeWaiting();
			if(info.data.code == 200){
				toast('配送成功！');
				plus.webview.getWebviewById(spage).reload();
				setTimeout(function(){
					ws.close();
				},1500)
			}else{
				toast(info.data.datas.error);
			}
						
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();
			error();
		}) 
	} 
	$scope.complete = function (order_id) {      //完成订单
		plus.nativeUI.showWaiting('进行中...');
		$http({
			method:"post",
			url:apiRoot + "?act=seller_order&op=order_complete",
			data:{
				key:key,
				order_id:order_id,
			},
			headers:{'Content-Type': 'application/x-www-form-urlencoded'},
	      	transformRequest: function (data) {
                    return $.param(data);
             }
		}).then(function SuccessCallback(info){ 
			console.log(JSON.stringify(info.data.code));
			plus.nativeUI.closeWaiting();
			if(info.data.code == 200){
				toast('操作成功！');
				plus.webview.getWebviewById(spage).reload();
				setTimeout(function(){
					ws.close();
				},1500)
			}else{
				toast(info.data.datas.error);
			}
						
		},function errorfunction(e){
			plus.nativeUI.closeWaiting();
			error();
		}) 
	}
	
	
})


document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	order_sn = ws.order_sn;
	spage = ws.spage;
	key = plus.storage.getItem('key');
	console.log(key+'--'+order_sn);
	aboutElement = document.querySelector('[ng-controller=appcontroller]');
	$scope = angular.element(aboutElement).scope();
	
	$.ajax({ 
		type:"post",
		url:apiRoot + "?act=seller_order&op=order_list",
		data:{
			key:key,
			order_sn:order_sn
			
		},
		dataType:'json',
		success:function(data){
//			console.log(JSON.stringify(data));
			if(data.code == 200){
				if(data.datas.order_list.length > 0){
					$scope.initView(data.datas.order_list);
					$scope.$apply();
				}else{
					toast('数据！');
				}
			}else{
				toast(data.datas.error);
			}
		},
		error:function(e){
			error();
		}
	});
	
	
},false)